package io.wins.vertx.service.impl;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.wins.vertx.entity.Comment;
import io.wins.vertx.service.BaseService;
import io.wins.vertx.service.CommentService;

public class CommentServiceImpl extends BaseService implements CommentService {
    public CommentServiceImpl(final Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public CommentService getCommentByConcertId(final String custId, final String conId, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select comm.*, cust.nickname, cust.image " +
                "from ds_comments comm " +
                "left join ds_customer cust on comm.customer_id = cust.id " +
                "where comm.concert_id = ? and (comm.customer_id = ? or comm.status = 1) order by comm.created_on desc")
            .put(PARAMS, new JsonArray().add(conId).add(custId));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public CommentService listComment(final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select comm.*, cust.nickname as nickname, cust.image as image, con.title as title " +
                "from ds_comments comm " +
                "left join ds_customer cust on comm.customer_id = cust.id " +
                "left join ds_concert con on con.id = comm.concert_id " +
                "order by comm.created_on desc;");
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public CommentService createComment(final Comment comment, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "insert into ds_comments (id, customer_id, concert_id, comment, score, created_on) " +
                "VALUES (?, ?, ?, ?, ?, UNIX_TIMESTAMP())")
            .put(PARAMS, new JsonArray()
                .add(comment.getId())
                .add(comment.getCustomerId())
                .add(comment.getConcertId())
                .add(comment.getComment())
                .add(comment.getScore()));
        this.insertSql(sqlMes, this.resultUpdate(comment.getId(), tMessage, "Error"));
        return this;
    }

    @Override
    public CommentService reviewComment(final JsonArray ids, final int status, final Message tMessage) {
        final StringBuilder sb = new StringBuilder();
        sb.append("update ds_comments set status = ? where id in (");
        int i = 0;
        while (i < ids.size()) {
            sb.append('?');
            i++;
            if (i<ids.size()) sb.append(',');
        }
        sb.append(')');
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, sb.toString())
            .put(PARAMS, new JsonArray()
                .add(status)
                .addAll(ids));
        this.updateSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public CommentService deleteComment(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "delete from ds_comments where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.deleteSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }
}
